// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import axios from 'axios'
// import VueSocketIO from 'vue-socket.io'
import App from './App'
import router from './router'
import i18n from './i18n/i18n'
const conf = require('../../../config.json')
Vue.config.productionTip = false
axios.defaults.withCredentials = true
axios.interceptors.response.use(
  response => {
    if (response.status === 200) {
      return Promise.resolve(response)
    } else {
      return Promise.reject(response)
    }
  },
  error => {
    console.log('ie', error, error.response, error.response.status)
    if (error.response.status) {
      switch (error.response.status) {
        case 332:
          localStorage.removeItem('login')
          router.push('/login')
          // eslint-disable-next-line prefer-promise-reject-errors
          return Promise.reject('请登录')
        case 404:
          // eslint-disable-next-line prefer-promise-reject-errors
          return Promise.reject('404')
        default:
          return Promise.reject(error.response)
      }
    }
  }
)
Vue.use(ElementUI)
Vue.prototype.$axios = axios

// router.beforeEach((to, from, next) => {
//   let localLoginInfo = localStorage.getItem('login')
//   console.log([localLoginInfo, to.fullPath])
//   Vue.prototype.$login = { role: localLoginInfo }
//   let freePath = ['/login', '/regist', '/login/resetPassword']
//   console.log((localLoginInfo === undefined), (localLoginInfo === null))
//   console.log()
//   if ((localLoginInfo === undefined) || (localLoginInfo === null)) {
//     // 没有登录信息，且不在freePath范围，要求进入login
//     if ((freePath.indexOf(to.fullPath.split('?')[0]) === -1)) {
//       next('/login')
//     } else {
//       next()
//     }
//   } else {
//     // 已登录
//     if ((freePath.indexOf(to.fullPath) !== -1)) {
//       next('/' + Vue.prototype.$login.role)
//     } else if (to.fullPath.indexOf('/' + Vue.prototype.$login.role) !== 0) {
//       next('/' + Vue.prototype.$login.role)
//     } else {
//       next()
//     }
//   }
// })

Vue.prototype.$EventBus = new Vue()

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  i18n,
  data: function () {
    return {
      URL: conf.apiHost,
      passwordRule: [
        { required: true, message: '请输入密码', transform: value => value, trigger: 'blur' },
        {
          type: 'string',
          message: '请输入不包含空格的字符',
          trigger: 'blur',
          transform(value) {
            if (value && value.indexOf(' ') === -1) {
              return value
            } else {
              return false
            }
          }
        },
        {
          trigger: 'blur',
          validator: (rule, value, callback) => {
            var passwordreg = /(?=.*\d)(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{6,16}/
            if (!passwordreg.test(value)) {
              callback(new Error('密码必须为6-16位，且包含数字、字母和特殊字符'))
            } else {
              callback()
            }
          }
        }
      ]
    }
  },
  components: { App },
  template: '<App/>',
  render: h => h(App)
})
